
package model;

import java.util.ArrayList;
import java.util.List;
import model.entities.//nameClass//;
import model.dao.//nameClass//DAO;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Expression;

public class //nameClass//Model implements //nameClass//DAO  {

    private static Configuration cfg = null;
    private static SessionFactory factory = null;
    private static //nameClass//Model instance = null;

    public static //nameClass//Model getInstance() {
        if (instance == null) {
            try {
                cfg = new Configuration().configure();
                factory = cfg.buildSessionFactory();
            } catch (HibernateException e) {
                System.out.println("Error //nameClass//Model getInstance : " + e.toString());
            }
            instance = new //nameClass//Model();
        }
        return instance;
    }

    public //nameClass//Model() {
    }

    public //nameClass// create(//nameClass// //nameclass//) {        
        Session session = null;
        try {
            session = factory.openSession();
            Transaction tx = session.beginTransaction();
            session.save(//nameclass//);
            tx.commit();
        } catch (HibernateException e) {
            System.out.println("Error //nameClass//sModel create : " + e.toString());
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (HibernateException e) {
                    System.out.println("Error //nameClass//sModel create : " + e.toString());
                }
            }
        }
        return //nameclass//;
    }

    public //nameClass// create(String id//nameparam//) {
        Session session = null;
        //nameClass// //nameclass// = null;
        try {
            session = factory.openSession();
            Transaction tx = session.beginTransaction();
            //nameclass// = new //nameClass//();
            //nameclass//.setId(Integer.parseInt(id));
            //nameasing//
            session.save(//nameclass//);
            tx.commit();
        } catch (HibernateException e) {
            System.out.println("Error //nameClass//sModel create : " + e.toString());
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (HibernateException e) {
                    System.out.println("Error //nameClass//sModel create : " + e.toString());
                }
            }
        }
        return //nameclass//;
    }

    public //nameClass// read(int id) {
        Session session = null;
        //nameClass// //nameclass// = null;
        Transaction tx = null;
        try {
            session = factory.openSession();
            tx = session.beginTransaction();
            Criteria criteria = session.createCriteria(//nameClass//.class);
            List list = criteria.add(Expression.eq("id", id)).list();
            if (list.isEmpty()) {
                throw new Exception("No user found for id: " + id);
            }
            //nameclass// = (//nameClass//) list.get(0);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (HibernateException e1) {
                    System.out.println("Error //nameClass//sModel read : " + e.toString());
                }
            }
            System.out.println("Error //nameClass//sModel read : " + e.toString());
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (HibernateException e) {
                    System.out.println("Error //nameClass//sModel read : " + e.toString());
                }
            }
        }
        return //nameclass//;
    }

    public void update(//nameClass// //nameclass//) {
        Session session = null;
        Transaction tx = null;
        try {
            session = factory.openSession();
            tx = session.beginTransaction();           
            session.update(//nameclass//);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (HibernateException e1) {
                    System.out.println("Error //nameClass//sModel update : " + e.toString());
                }
            }
            System.out.println("Error //nameClass//sModel update : " + e.toString());
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (HibernateException e) {
                    System.out.println("Error //nameClass//sModel update : " + e.toString());
                }
            }
        }
    }

    public void delete(//nameClass// //nameclass//) {
        Session session = null;
        Transaction tx = null;
        try {
            session = factory.openSession();
            tx = session.beginTransaction();
            session.delete(//nameclass//);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (HibernateException e1) {
                    System.out.println("Error //nameClass//sModel delete : " + e.toString());
                }
            }
            System.out.println("Error //nameClass//sModel delete : " + e.toString());
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (HibernateException e) {
                    System.out.println("Error //nameClass//sModel delete : " + e.toString());
                }
            }
        }
    }

    public ArrayList<//nameClass//> find() {
        Session session = null;
        Transaction tx = null;
        try {
            session = factory.openSession();
            tx = session.beginTransaction();
            return (ArrayList<//nameClass//>) session.createQuery("from //nameClass//").list();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            System.out.println("Error //nameClass//sModel find : " + e.toString());
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (HibernateException e) {
                    System.out.println("Error //nameClass//sModel find : " + e.toString());
                }
            }
        }
        return null;
    }
}
